package com.jy.adscm.zltx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.bean.system.JyUser;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.system.mapper.mysql.JyUserMapper;
import com.jy.adscm.zltx.bean.SjApplyUpdateRecord;
import com.jy.adscm.zltx.bean.SjApprovalRecord;
import com.jy.adscm.zltx.bean.SjFile;
import com.jy.adscm.zltx.bean.SjFileDept;
import com.jy.adscm.zltx.mapper.mysql.SjApplyUpdateRecordMapper;
import com.jy.adscm.zltx.mapper.mysql.SjApprovalRecordMapper;
import com.jy.adscm.zltx.mapper.mysql.SjFileDeptMapper;
import com.jy.adscm.zltx.mapper.mysql.SjFileMapper;
import com.jy.adscm.zltx.service.SjApplyUpdateRecordService;
import com.jy.adscm.zltx.service.SjApprovalRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zyl
 * @since 2018-11-08
 */
@Service
public class SjApplyUpdateRecordServiceImpl extends ServiceImpl<SjApplyUpdateRecordMapper, SjApplyUpdateRecord> implements SjApplyUpdateRecordService {

     @Autowired
    SjFileDeptMapper sjFileDeptMapper;
     @Autowired
    SjFileMapper sjFileMapper;
     @Autowired
    SjApprovalRecordMapper SjApprovalRecordMapper;
    @Autowired
    JyUserMapper  jyUserMapper;
    @Override
    public boolean insertSjApplyUpdateRecord(SjApplyUpdateRecord sjApplyUpdateRecord) throws AdscmSystemException {
        SjFile sjFile = sjFileMapper.selectById(sjApplyUpdateRecord.getFileId());
        if (sjFile==null || "0".equals(sjFile.getIsApproval())){
            throw  new AdscmSystemException("不存在或者未发布");
        }
        //把原始内容新增到修改申请里面
        boolean insert = super.insert(sjApplyUpdateRecord);
        //生成审核记录
        Wrapper<SjFileDept> wrapper=new EntityWrapper<>();
        wrapper.eq("fileId",sjApplyUpdateRecord.getFileId());
        List<SjFileDept> sjFileDepts =sjFileDeptMapper.selectList(wrapper);
        if (sjFileDepts.size()>0){
            for (SjFileDept sjFileDept : sjFileDepts) {
                SjApprovalRecord sjApprovalRecord= new SjApprovalRecord();
                sjApprovalRecord.setDeptId(sjFileDept.getDeptId());
                sjApprovalRecord.setUpdateId(sjApplyUpdateRecord.getId());
                sjApprovalRecord.setFileId(sjApplyUpdateRecord.getFileId());
                SjApprovalRecordMapper.insert(sjApprovalRecord);
            }
        } else {
            throw new  AdscmSystemException("未找到相关部门");
        }


        return insert;
    }

    @Override
    public List<SjApplyUpdateRecord> selectByFileId(Integer fileId) {
        Wrapper<SjApplyUpdateRecord>  wrapper =new EntityWrapper<>();
        wrapper.eq("fileId",fileId);
        List<SjApplyUpdateRecord> sjApplyUpdateRecords = super.selectList(wrapper);
        for (SjApplyUpdateRecord sjApplyUpdateRecord : sjApplyUpdateRecords) {
            if (sjApplyUpdateRecord.getApplicant()!=null&&!"".equals(sjApplyUpdateRecord.getApplicant())){
                JyUser jyUser = jyUserMapper.selectById(sjApplyUpdateRecord.getApplicant());
                sjApplyUpdateRecord.setApplicantName(jyUser.getName());
            }
        }
        return sjApplyUpdateRecords;
    }
}
